package com.windowes.weichang.controller.common;

import com.windowes.weichang.access.AccessLimit;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.pojo.WUser;
import com.windowes.weichang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/5/23 下午9:09
 * @description:
 *
 **/
@RestController
@Api(description = "用户登陆相关API")
public class LoginController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "登陆", notes = "非平台管理人员调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "账号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form", dataType = "string")
    })
    @PostMapping("login")
    public ServerResponse login(HttpServletResponse response, String phone, String password) {
        return userService.login(response, phone, password);
    }

    @ApiOperation(value = "后台登陆", notes = "平台人员")
    @ApiImplicitParam(name = "user", value = "用户", required = true, dataType = "WUser")
    @PostMapping("login4Manage")
    public ServerResponse login4Manage(HttpServletResponse response, @RequestBody WUser user) {
        return userService.login4Manage(response, user.getPhone(), user.getPassword());
    }

    @ApiOperation(value = "通过验证码登陆", notes = "非平台调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "账号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "smsCode", value = "验证码", required = true, paramType = "form", dataType = "string")
    })
    @PostMapping("loginWithSmsCode")
    public ServerResponse loginWithSmsCode(HttpServletResponse response, String phone, String smsCode) {
        return userService.loginWithSmsCode(response, phone, smsCode);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "账号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "smsCode", value = "验证码", required = true, paramType = "form", dataType = "string")
    })
    @ApiOperation(value = "用户注册", notes = "非平台,手机号及验证码,默认密码123456")
    @PostMapping("register")
    public ServerResponse register(HttpServletResponse response, String phone, String smsCode) {
        return userService.register(response, phone, smsCode);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "账号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "smsCode", value = "验证码", required = true, paramType = "form", dataType = "string")
    })
    @ApiOperation(value = "重置密码", notes = "")
    @PostMapping("resetPassword")
    public ServerResponse resetPassword(String phone, String smsCode, String password) {
        return userService.resetPassword(phone, smsCode, password);
    }


    @AccessLimit
    @ApiOperation(value = "退出登录", notes = "")
    @PostMapping("loginOff")
    public ServerResponse loginOff(HttpServletResponse response) {
        return userService.loginOff();
    }
}
